package com.dinadi.yidi.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.UserInfoBean;
import com.dinadi.yidi.bean.eventbus.EventUserInfo;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.ui.MyApplication;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;

import java.util.Timer;
import java.util.TimerTask;

import de.greenrobot.event.EventBus;

/**
 * author: JiaYu.He
 * Date: 2017/3/30    修改用户资料
 * Time: 12:30
 */

public class EditUserDataActivity extends BaseActivity {
    private View view;
    private TextView tvTitle;
    private EditText user_data;
    private String usertype, userID;
    private TextView text_title_right;
    private UserInfoBean bean;
    private String userDate;
    private String token;

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(EditUserDataActivity.this).inflate(R.layout.activity_edit_user, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        text_title_right = (TextView) view.findViewById(R.id.text_title_right);
        text_title_right.setText(getResources().getString(R.string.text_save));
        text_title_right.setTextColor(getResources().getColor(R.color.color_999999));
        user_data = (EditText) view.findViewById(R.id.user_data);
        /*自动弹出键盘*/
        user_data.setFocusableInTouchMode(true);
        user_data.requestFocus();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

                           public void run() {
                               InputMethodManager inputManager =
                                       (InputMethodManager) user_data.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                               inputManager.showSoftInput(user_data, 0);
                           }

                       },
                200);

    }

    @Override
    protected void initListener() {
        registerClickListener(text_title_right);
    }

    @Override
    public void onResume() {
        super.onResume();
         /*用户ID*/

        userID = SharePreferencesHelper.getInstance(EditUserDataActivity.this).getString("userId");
        token = SharePreferencesHelper.getInstance(EditUserDataActivity.this).getString("token");
       Intent intent = getIntent();
        String nickName = intent.getStringExtra("nickName");
        if (nickName != null) {
            user_data.setText(nickName);
            user_data.setSelection(nickName.length());//将光标移至文字末尾
        }
       String name = intent.getStringExtra("name");
        if (name != null) {
            user_data.setText(name);
            user_data.setSelection(name.length());//将光标移至文字末尾

        }
        String position = intent.getStringExtra("position");
        if (position != null) {
            user_data.setText(position);
            user_data.setSelection(position.length());//将光标移至文字末尾

        }

       String autograph = intent.getStringExtra("autograph");
        if (autograph != null) {
            user_data.setText(autograph);
            user_data.setSelection(autograph.length());//将光标移至文字末尾

        }
        usertype = intent.getStringExtra("usertype");
        if (usertype.equals("1")) {
                    /*昵称*/
            tvTitle.setText(getResources().getString(R.string.edit_text_nickname));
        } else if (usertype.equals("2")) {
                    /*姓名*/
            tvTitle.setText(getResources().getString(R.string.edit_text_name));
        } else if (usertype.equals("3")) {
                    /*职位*/
            tvTitle.setText(getResources().getString(R.string.edit_text_zhiwei));
        } else {
                     /*签名*/
            tvTitle.setText(getResources().getString(R.string.edit_text_qm));
        }

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.text_title_right:
                userDate = user_data.getText().toString();
                bean = new UserInfoBean();
                if (usertype.equals("1")) {
                    /*昵称*/
                    bean.setNickName(userDate);
                } else if (usertype.equals("2")) {
                    /*姓名*/
                    bean.setUserName(userDate);
                } else if (usertype.equals("3")) {
                    /*职位*/
                    bean.setBrandName(userDate);
                } else {
                     /*签名*/
                    bean.setSignature(userDate);
                }

                bean.setUserId(userID);
                bean.setToken(token);
                CommonInterface userDataEditingInterface = new CommonInterface(EditUserDataActivity.this, httpListener);
                userDataEditingInterface.getInterface("11001", JSON.toJSONString(bean));
                break;
            default:
                break;
        }
    }

    HttpListener httpListener = new HttpListener() {
        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                toast("修改成功");
                if (usertype.equals("1")) {
                    /*昵称*/
                    bean.setNickName(userDate);
                    MyApplication.getApp().getUser().setNickName(userDate);
                } else if (usertype.equals("2")) {
                    /*姓名*/
                    bean.setUserName(userDate);
                    MyApplication.getApp().getUser().setUserName(userDate);
                } else if (usertype.equals("3")) {
                    /*职位*/
                    bean.setBrandName(userDate);
                    MyApplication.getApp().getUser().setBrandName(userDate);
                } else {
                     /*签名*/
                    bean.setSignature(userDate);
                    MyApplication.getApp().getUser().setSignature(userDate);
                }

                EventBus.getDefault().post(new EventUserInfo(bean));
                EditUserDataActivity.this.finish();
            }
        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            toast("修改失败");
        }
    };
}
